import { InjectionKey } from "vue"
import type { RuleItem, ValidateError, ValidateFieldsError } from 'async-validator'

export interface FormItemRule extends RuleItem {
    trigger?: string
}
export type FormRules = Record<string, FormItemRule[]>

export interface FormProps {
    model: Record<string, any> // 所有的表单数据
    rules?: FormRules
}

export interface FormItemProps {
    label: string
    prop?: string
}

export interface FormItemContext {
    // validate: (trigger: string) => Promise<boolean>
    prop: string
    validate: (trigger?: string) => Promise<any>
    resetField: () => void
    clearValidate: () => void
}

export interface FormContext extends FormProps {
    addField: (field: FormItemContext) => void
    removeField: (field: FormItemContext) => void
}

export interface ValidateStatusProp {
    status: 'init' | 'success' | 'error'
    errorMessage: string
    loading: boolean
}

export interface FormValidateFailure {
    errors: ValidateError[] | null
    fields: ValidateFieldsError
}

export interface FormInstance {
    validate: () => Promise<any>
    resetFields: (props?: string[]) => void,
    clearValidate: (props?: string[]) => void
}

export interface FormItemInstance {
    validateStatus: ValidateStatusProp
    validate: (trigger?: string) => Promise<any>
    resetField: () => void
    clearValidate: () => void
}

// InjectionKey 是 Vue 3 中的一个类型，用于定义一个注入键（injection key）。这个键用于在组件树中传递数据，通常与 provide 和 inject 一起使用。
export const formContextKey: InjectionKey<FormContext> = Symbol('formContextKey')
export const formItemContextKey: InjectionKey<FormItemContext> = Symbol('formItemContextKey')


